#include "SkinProperty.h"
#include <tchar.h>

CSkinProperty::CSkinProperty( void )
{
    m_skinPropertyType = SKIN_PROPERTY_UNKNOWN;
    m_nPropertyIndex = SKIN_PROPERTY_INDEX_NULL;
}


CSkinProperty::~CSkinProperty( void )
{
    ClearProperty();
}

void CSkinProperty::SetPropertyColor( LPCTSTR lpszValue )
{
    ClearProperty();

    int iLen = _tcslen( lpszValue ) * sizeof( TCHAR );
    TCHAR* lpszTemp = new TCHAR[iLen + 1];
    _tcscpy_s( lpszTemp, iLen + 1, lpszValue );
    lpszTemp[iLen] = _T( '\0' );

    int r = 0;
    int g = 0;
    int b = 0;
    LPCTSTR lpszStr = lpszValue;

    while ( _T( '\0' ) != *lpszStr )
    {
        TCHAR* pos = ( TCHAR* )_tcschr( lpszStr, _T( ',' ) );

        if ( NULL != pos )
        {
            lpszTemp[pos - lpszValue] = _T( ' ' );
            lpszStr += pos - lpszValue;
        }

        lpszStr++;
    }

    _stscanf_s( lpszTemp, _T( "%i %i %i" ), &r, &g, &b );
    delete[] lpszTemp;
    lpszTemp = NULL;
    m_clrVal = RGB( r, g, b );
}

void CSkinProperty::SetPropertyString( LPCTSTR lpszValue )
{
    ClearProperty();

    int nLen = _tcslen( lpszValue ) * sizeof( TCHAR );
    m_lpszVal = new TCHAR[nLen + 1];
    _tcscpy_s( m_lpszVal, nLen + 1, lpszValue );
//	m_nPropertyCode = SKIN_PROPERTY_STRING;
}

void CSkinProperty::SetPropertyBool( LPCTSTR lpszValue )
{
    ClearProperty();

    m_bVal = _tcsicmp( lpszValue, _T( "TRUE" ) ) == 0;
}

void CSkinProperty::SetPropertyInt( LPCTSTR lpszValue )
{
    ClearProperty();
    m_iVal = _ttoi( lpszValue );
}

void CSkinProperty::SetPropertyEnum( int nEnumValue )
{
    ClearProperty();
    m_iVal = nEnumValue;
}

void CSkinProperty::SetPropertySize( LPCTSTR lpszValue )
{
    ClearProperty();
    _stscanf_s( lpszValue, _T( "%i,%i" ), &m_szVal.cx, &m_szVal.cy );
}

void CSkinProperty::SetPropertyRect( LPCTSTR lpszValue )
{
    ClearProperty();
    _stscanf_s( lpszValue, _T( "%ld,%ld,%ld,%ld" ), &m_rcVal.left, &m_rcVal.right, &m_rcVal.top, &m_rcVal.bottom );
}

void CSkinProperty::SetPropertyFont( LPCTSTR lpszValue )
{
    ClearProperty();
    m_lfVal = new LOGFONT;
    ZeroMemory( m_lfVal, sizeof( LOGFONT ) );

    HDC hWndDC =::GetWindowDC( NULL );
    m_lfVal->lfCharSet = ( BYTE )::GetTextCharsetInfo( hWndDC, NULL, 0 );

    TCHAR chBold[50];
    memset( chBold, 0, 50 * sizeof( TCHAR ) );
    _stscanf_s( lpszValue, _T( "%[^,], %d, %ld, %s" ), m_lfVal->lfFaceName, LF_FACESIZE, &m_lfVal->lfHeight, chBold );
    m_lfVal->lfWeight = _tcsicmp( chBold, _T( "BOLD" ) ) != 0 ?  FW_NORMAL : FW_BOLD;

    int ppi = GetDeviceCaps( hWndDC, LOGPIXELSY );

    m_lfVal->lfHeight = - MulDiv( m_lfVal->lfHeight, ppi, 72 );
    ::ReleaseDC( NULL, hWndDC );
}

void CSkinProperty::ClearProperty()
{
    if ( m_skinPropertyType == SKIN_PROPERTY_STRING )
        delete[] m_lpszVal;

    if ( m_skinPropertyType == SKIN_PROPERTY_FONT )
        delete m_lfVal;

    m_skinPropertyType = SKIN_PROPERTY_UNKNOWN;
}

// BOOL CSkinProperty::operator==(const CSkinProperty& obj) const
// {
// 	if (obj==*this)
// 	{
// 		return TRUE;
// 	}
// 	if (obj.m_skinPropertyType!=m_skinPropertyType)
// 	{
// 		return FALSE;
// 	}
// 	switch (m_skinPropertyType)
// 	{
// 		case SKIN_PROPERTY_COLOR:    return m_clrVal == obj.m_clrVal;
// 		case SKIN_PROPERTY_STRING:   return _tcsicmp(m_lpszVal, obj.m_lpszVal) == 0;
// 		case SKIN_PROPERTY_BOOL:     return m_bVal == obj.m_bVal;
// 		case SKIN_PROPERTY_RECT:     return ::EqualRect(&obj.m_rcVal, &m_rcVal);
// 		case SKIN_PROPERTY_INT:      return m_iVal == obj.m_iVal;
// 		case SKIN_PROPERTY_POSITION: return (m_szVal.cx == obj.m_szVal.cx) && (m_szVal.cy == obj.m_szVal.cy);
// //		case SKIN_PROPERTY_ENUM:     return m_iVal == obj.m_iVal;
// 		case SKIN_PROPERTY_FONT:     return memcmp(m_lfVal, obj.m_lfVal, sizeof(LOGFONT)) == 0;
// 	}
// 	return FALSE;
// }


void CSkinProperty::SetPropertyType( SKIN_PROPERTY_TYPE proType )
{
    m_skinPropertyType = proType;
}

void CSkinProperty::SetPropertyIndex( SKIN_PROPERTY_INDEX proCode )
{
    m_nPropertyIndex = proCode;
}

SKIN_PROPERTY_INDEX CSkinProperty::GetPropertyIndex()
{
    return m_nPropertyIndex;
}

SKIN_PROPERTY_TYPE CSkinProperty::GetPropertyType()
{
    return m_skinPropertyType;
}
